// pages/logonzc/index.js
const app = getApp();
const {
  Http
} = require("../../utils/http")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    phone: '',
    shop_title: '请选择门店(必选项)',
    store_id: '',
    store_name: '',
    field_name:'',
    field_phone:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.login((res) => { //页面初始化
      console.log(res);
      wx.setStorageSync('register', res.register);
      wx.setStorageSync('shoplist', res.store_list);
      wx.setStorageSync('verifyman', res.verification);
      wx.setStorageSync('distributor', res.distributor);
      wx.setStorageSync('user_id', res.user_id);
      wx.setStorageSync('store_id', res.store_id);
      wx.setStorageSync('money', res.userinfo.money);
      wx.setStorageSync('payment_password', res.userinfo.payment_password);
      wx.hideLoading();
      console.log('成功');
    });

    wx.setStorageSync('storeName', '')
    wx.setStorageSync('storeId', '')

  },

  //验证方法
  getPhoneNumber: async function () {
    if (this.data.store_name == '') {
      wx.showToast({
        title: '请选择门店',
        duration: 2000,
        icon: 'none'
      });
    } else if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(this.data.value))) {
      wx.showToast({
        title: '姓名有误',
        duration: 2000,
        icon: 'none'
      });
    } else if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$/.test(this.data.phone))) {
      wx.showToast({
        title: '手机号码有误',
        duration: 2000,
        icon: 'none'
      });
    } else {
      let result = await Http.request({
        url: 'wechat/basics/register_user_distributor_auth',
        data: {
          store_id: this.data.store_id,
          username: this.data.value,
          mobile: this.data.phone
        },
        method: "POST"
      });
      if (result.code == 0) {
        wx.showToast({
          title: '注册成功！',
          icon: 'success',
          duration: 3000
        });
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/home/index',
          })
        }, 3000)

      } else {
        wx.showToast({
          title: result.msg,
          icon: 'none', //当icon：'none'时，没有图标 只有文字
          duration: 2000
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (Object.prototype.toString.call(wx.getStorageSync('storeId')) !== '[object Undefined]') {
      this.setData({
        shop_title: '你选择了',
        store_name: wx.getStorageSync('storeName'),
        store_id: wx.getStorageSync('storeId')
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})